﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Entity.YiDongYuan;
using YiSha.Model.Param.YiDongYuan;
using YiSha.Service.YiDongYuan;
using YiSha.Entity.OrganizationManage;
using YiSha.Service.OrganizationManage;
using YiSha.Model.Param.OrganizationManage;

namespace YiSha.Business.YiDongYuan
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2021-06-09 08:20
    /// 描 述：检测任务业务类
    /// </summary>
    public class DetectionTaskBLL
    {
        private DetectionTaskService detectionTaskService = new DetectionTaskService();
        private UserService userService = new UserService();
        private DetectionInfoService infoService = new DetectionInfoService();

        #region 获取数据

        public async Task<TData<List<RegionReportEntity>>> GetRegionList(string type)
        {
            TData<List<RegionReportEntity>> obj = new TData<List<RegionReportEntity>>();
            obj.Data = await detectionTaskService.GetRegionList(type);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<long> GetRecheclCount(int detectionType)
        {
            return await detectionTaskService.GetRecheclCount(detectionType);
        }

        public async Task<TData<List<DetectionTaskEntity>>> GetList(DetectionTaskListParam param)
        {
            TData<List<DetectionTaskEntity>> obj = new TData<List<DetectionTaskEntity>>();
            obj.Data = await detectionTaskService.GetList(param);
            await BindRelationInfo(obj);

            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<DetectionTaskEntity>>> GetListByUserId(long userId)
        {
            TData<List<DetectionTaskEntity>> obj = new TData<List<DetectionTaskEntity>>();
            obj.Data = await detectionTaskService.GetListByUserId(userId);
            MechanicsBLL mBLL = new MechanicsBLL();
            DetectionMechanicsBLL dmBLL = new DetectionMechanicsBLL();
            var dmList = await dmBLL.GetList(new DetectionMechanicsListParam() { Ids = obj.Data.Select(t => t.MechanicsId.Value).ParseToStrings<long>() });
            foreach (var item in obj.Data)
            {
                var entity = dmList.Data.Find(t => t.Id == item.MechanicsId);
                if (entity != null)
                {
                    item.OwnerName = entity.OwnerName;
                    item.OwnerTel = entity.OwnerTel;
                }
            }
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        private async Task BindRelationInfo(TData<List<DetectionTaskEntity>> obj)
        {
            List<UserEntity> userList = await userService.GetList(new UserListParam { });
            List<DetectionInfoEntity> list = await infoService.GetList(new DetectionInfoListParam { Ids = string.Join(",", obj.Data.Select(p => p.DetectionInfoId.ParseToString()).ToArray()) });
            foreach (DetectionTaskEntity entity in obj.Data)
            {
                if (entity.AuditUserId > 0)
                {
                    entity.AuditUserName = userList.Where(p => p.Id == entity.AuditUserId).Select(p => p.RealName).FirstOrDefault();
                }
                else
                {
                    entity.AuditUserName = string.Empty;
                }
                if (entity.DetectionUserId > 0)
                {
                    entity.DetectionUserName = userList.Where(p => p.Id == entity.DetectionUserId).Select(p => p.RealName).FirstOrDefault();
                }
                else
                {
                    entity.DetectionUserName = string.Empty;
                }
                if (entity.DetectionInfoId > 0)
                {
                    entity.DetectionInfoResult = list.Where(p => p.Id == entity.DetectionInfoId).Select(p => p.Result).FirstOrDefault();
                    entity.DetectionInfoResultDPQ = list.Where(p => p.Id == entity.DetectionInfoId).Select(p => p.ResultDPQ).FirstOrDefault();
                    entity.SiteVideo = list.Where(p => p.Id == entity.DetectionInfoId).Select(p => p.VideoId).FirstOrDefault().ToString();
                }
            }
        }

        private async Task BindRelationInfo(TData<List<DetectionTaskEntity>> obj, List<DetectionInfoEntity> infoList)
        {
            List<UserEntity> userList = await userService.GetList(new UserListParam { });
            foreach (DetectionTaskEntity entity in obj.Data)
            {
                var info = infoList.Find(t => t.Id == entity.DetectionInfoId);
                if (info != null)
                {
                    entity.DetectionInfoResult = info.Result;
                    entity.DetectionInfoResultDPQ = info.ResultDPQ;
                }
                if (entity.AuditUserId > 0)
                {
                    entity.AuditUserName = userList.Where(p => p.Id == entity.AuditUserId).Select(p => p.RealName).FirstOrDefault();
                }
                else
                {
                    entity.AuditUserName = string.Empty;
                }
                if (entity.DetectionUserId > 0)
                {
                    entity.DetectionUserName = userList.Where(p => p.Id == entity.DetectionUserId).Select(p => p.RealName).FirstOrDefault();
                }
                else
                {
                    entity.DetectionUserName = string.Empty;
                }
                if (entity.DetectionInfoId > 0)
                {
                    var infoEntity = infoService.GetEntity(entity.DetectionInfoId.Value).Result;
                    entity.DetectionInfoResult = infoEntity.Result;
                    entity.DetectionInfoResultDPQ = infoEntity.ResultDPQ;
                }
            }
        }

        public async Task<TData<List<DetectionTaskEntity>>> GetPageList(DetectionTaskListParam param, Pagination pagination)
        {
            TData<List<DetectionTaskEntity>> obj = new TData<List<DetectionTaskEntity>>();
            obj.Data = await detectionTaskService.GetPageList(param, pagination);
            await BindRelationInfo(obj);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<ProcessDataEntity>>> GetListByInfoId(long id)
        {
            TData<List<ProcessDataEntity>> obj = new TData<List<ProcessDataEntity>>();
            obj.Data = await detectionTaskService.GetListByInfoId(id);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<string>> UpdateTaskAudit(string id, int auditReason, string aduitReason, long? auditUserId)
        {
            TData<string> obj = new TData<string>();
            await detectionTaskService.Audit(id, auditReason, aduitReason, auditUserId);
            obj.Data = id.ToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<DetectionTaskEntity>> GetEntity(long id)
        {
            TData<DetectionTaskEntity> obj = new TData<DetectionTaskEntity>();
            obj.Data = await detectionTaskService.GetEntity(id);
            if (obj.Data != null)
            {
                await BindEntityRelationInfo(obj);
                obj.Tag = 1;
            }
            return obj;
        }

        public async Task<TData<DetectionTaskEntity>> GetLastEntityByCode(string code)
        {
            TData<DetectionTaskEntity> obj = new TData<DetectionTaskEntity>();
            obj.Data = await detectionTaskService.GetLastEntityByCode(code);
            if (obj.Data != null)
            {
                await BindEntityRelationInfo(obj);
                obj.Tag = 1;
            }
            return obj;
        }

        private async Task BindEntityRelationInfo(TData<DetectionTaskEntity> obj)
        {
            List<UserEntity> userList = await userService.GetList(new UserListParam { });
            List<DetectionInfoEntity> list = await infoService.GetList(new DetectionInfoListParam { });
            if (obj.Data.AuditUserId > 0)
            {
                obj.Data.AuditUserName = userList.Where(p => p.Id == obj.Data.AuditUserId).Select(p => p.RealName).FirstOrDefault();
            }
            else
            {
                obj.Data.AuditUserName = string.Empty;
            }
            if (obj.Data.DetectionUserId > 0)
            {
                obj.Data.DetectionUserName = userList.Where(p => p.Id == obj.Data.DetectionUserId).Select(p => p.RealName).FirstOrDefault();
            }
            else
            {
                obj.Data.DetectionUserName = string.Empty;
            }
            if (obj.Data.DetectionInfoId > 0)
            {
                obj.Data.DetectionInfoResult = list.Where(p => p.Id == obj.Data.DetectionInfoId).Select(p => p.Result).FirstOrDefault();
                obj.Data.DetectionInfoResultDPQ = list.Where(p => p.Id == obj.Data.DetectionInfoId).Select(p => p.ResultDPQ).FirstOrDefault();
            }
        }

        #endregion 获取数据

        #region 提交数据

        public async Task<TData<string>> SaveForm(DetectionTaskEntity entity)
        {
            TData<string> obj = new TData<string>();
            await detectionTaskService.SaveForm(entity);
            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<string>> SaveRecheckForm(RecheckEntity entity)
        {
            TData<string> obj = new TData<string>();
            await detectionTaskService.SaveRecheckForm(entity);
            obj.Data = entity.DetectionUserId.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<string>> SaveChangeCode(long id, string newCode)
        {
            TData<string> obj = new TData<string>();
            await detectionTaskService.SaveChangeCode(id, newCode);
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await detectionTaskService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }

        public TData Upload2Hzx(long id)
        {
            TData obj = new TData();
            detectionTaskService.Upload2Hzx(id);
            obj.Tag = 1;
            return obj;
        }

        #endregion 提交数据
    }
}
